package com.example.taobaounion.ui.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.taobaounion.model.bean.Categories;
import com.example.taobaounion.ui.fragment.HomePagerFragment;

import java.util.ArrayList;
import java.util.List;
//分类适配器
public class HomePagerAdapter extends FragmentPagerAdapter {


    private List<Categories.DataBean> categoriesList=new ArrayList<>();

    public HomePagerAdapter(@NonNull  FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }


    @Override
    public CharSequence getPageTitle(int position) {

        return categoriesList.get(position).getTitle();
    }

    //返回Fragment
    @Override
    public Fragment getItem(int position) {
        //LogUtils.d(this,"position->"+position);
        Categories.DataBean dataBean = categoriesList.get(position);
        HomePagerFragment homePagerFragment = HomePagerFragment.newInstance(dataBean);
        return homePagerFragment;//homePagerFragment,开始执行homePagerFragment的onCreateView
    }

    @Override
    public int getCount() {
        return categoriesList.size();
    }

    public void setCategories(Categories categories) {

        categoriesList.clear();
        List<Categories.DataBean> data = categories.getData();
        categoriesList.addAll(data);
        //LogUtils.d(this,"size"+this.categoriesList.size());
        notifyDataSetChanged();
    }
}
